package kinect.servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;

import dll_test.KinectHandler;

/**
 * Servlet implementation class GetDepthFrameServlet
 */
@WebServlet("/GetDepthFrameServlet")
public class GetDepthFrameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static KinectHandler kHandler = KinectHandler.getInstance();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetDepthFrameServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if(!kHandler.isInitiated()) kHandler.init();

		response.setContentType("image/jpg");
		ServletOutputStream out = response.getOutputStream();
		kHandler.updateDepthFrame();
		BufferedImage img = kHandler.getDepthFrameInBufferedImage();
		JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(img);
		JPEGCodec.createJPEGEncoder(out, param).encode(img, param);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
